<?php


namespace app\admin\model;


use app\admin\model\CustomerServiceAccountMiddle;
use think\Model;

class User extends Model
{

    //自动写入时间
    protected $autoWriteTimestamp = true;

    /**
     * 时间格式化
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i:s';

    // 其他时间戳字段格式化
    protected $type = [
        'logintime' => 'timestamp',
        'jointime' => 'timestamp',
        'prevtime' => 'timestamp',
    ];

    public function getUserAllByWhere($where = [], $field = '*')
    {
        return self::field($field)->where($where)->select()->toArray();
    }

    /**
     * 格式化订单数据
     * @param $list
     */
    public function formatOrderData($list)
    {
        if (empty($list)) {
            return $list;
        }
        $userIds = array_column($list, 'user_id');
        //去重
        $userIds = array_flip($userIds);
        $userIds = array_keys($userIds);
        $where = [
            'id' => ['in', $userIds]
        ];
        $userList = $this->getUserAllByWhere($where, 'id,username,mobile,jointime,customer_service_account_middle_id,jointime');
        $userList = $this->formatUserData($userList);
        $userList = array_column($userList, null, 'id');
        //取出所有应用列表
        $appBaseList = AppBase::select()->toArray();
        $appBaseList = array_column($appBaseList, null, 'id');
        foreach ($list as $key => $value) {
            $value['username'] = empty($userList[$value['user_id']]) ? '' : $userList[$value['user_id']]['username'];
            $value['mobile'] = empty($userList[$value['user_id']]) ? '' : $userList[$value['user_id']]['mobile'];
            $value['jointime'] = empty($userList[$value['user_id']]) ? '' : $userList[$value['user_id']]['jointime'];
            $value['kf_name'] = empty($userList[$value['user_id']]) ? '' : $userList[$value['user_id']]['kf_name'];
            $value['kf_username'] = empty($userList[$value['user_id']]) ? '' : $userList[$value['user_id']]['kf_username'];
            $value['app_base_title'] = empty($appBaseList[$value['app_base_id']]) ? '' : $appBaseList[$value['app_base_id']]['title'];
            $list[$key] = $value;
        }
        return $list;
    }

    /**
     * 格式化客服
     * @param $userList
     * @return
     */
    public function formatUserData($userList)
    {
        $userList = array_column($userList, null, 'id');
        //查找客服中间表
        $customerServiceAccountMiddleIds = array_column($userList, 'customer_service_account_middle_id');
        //去重
        $customerServiceAccountMiddleIds = array_flip($customerServiceAccountMiddleIds);
        $customerServiceAccountMiddleIds = array_keys($customerServiceAccountMiddleIds);
        //查找客服中间表
        $AccountMiddleWhere = [
            'id' => ['in', $customerServiceAccountMiddleIds]
        ];
        $customerServiceAccountMiddleList = CustomerServiceAccountMiddle::where($AccountMiddleWhere)->select()->toArray();

        //查找客服
        $accountIds = array_column($customerServiceAccountMiddleList, 'customer_service_account_id');
        //去重
        $accountIds = array_flip($accountIds);
        $accountIds = array_keys($accountIds);
        $accountWhere = [
            'id' => ['in', $accountIds]
        ];
        $accountList = CustomerServiceAccount::where($accountWhere)->select()->toArray();
        $accountList = array_column($accountList, null, 'id');
        //客服人员
        $customerServicePersonnelIds = array_column($accountList, 'customer_service_personnel_id');
        $customerServicePersonnelWhere = [
            'id' => ['in', $customerServicePersonnelIds]
        ];
        $customerServicePersonnelList = CustomerServicePersonnel::where($customerServicePersonnelWhere)->select()->toArray();
        $customerServicePersonnelList = array_column($customerServicePersonnelList, null, 'id');
        //客服映射到客服中间表
        foreach ($customerServiceAccountMiddleList as $key => $value) {
            $value['username'] = empty($accountList[$value['customer_service_account_id']]) ? '' : $accountList[$value['customer_service_account_id']]['username'];
            $value['kf_username'] = empty($customerServicePersonnelList[$accountList[$value['customer_service_account_id']]['customer_service_personnel_id']]) ? '' : $customerServicePersonnelList[$accountList[$value['customer_service_account_id']]['customer_service_personnel_id']]['name'];
            $customerServiceAccountMiddleList[$key] = $value;
        }
        //中间表映射到用户
        $customerServiceAccountMiddleList = array_column($customerServiceAccountMiddleList, null, 'id');
        foreach ($userList as $key => $value) {
            $value['kf_name'] = empty($customerServiceAccountMiddleList[$value['customer_service_account_middle_id']]) ? '' : $customerServiceAccountMiddleList[$value['customer_service_account_middle_id']]['username'];
            $value['kf_username'] = empty($customerServiceAccountMiddleList[$value['customer_service_account_middle_id']]) ? '' : $customerServiceAccountMiddleList[$value['customer_service_account_middle_id']]['kf_username'];
            $userList[$key] = $value;
        }
        //获取客服人员名字
        return $userList;
    }
}